﻿	<?
    	session_start();
		if($_REQUEST['date'] != ""){
			$sort = " sub.log_date ".$_REQUEST['date'];
		}else if($_REQUEST['pview'] != ""){
			$sort = " sub.pview ".$_REQUEST['pview'];
		}else{
			$sort = " sub.pview ".$_REQUEST['pview'];
		}
	?>
	<style type="text/css">
		.answer_panel{
			display:none;	
		}
		.btnanswer{
			cursor:pointer;
			color:#00CC33
		}
		.btnanswered{
			cursor:pointer;
			color:red;
		}
		td , th{
			padding:5px 5px 5px 5px;	
		}
	</style>
    <h2>Page Views</h2>
	<?
			if($_SERVER['HTTP_HOST'] == "localhost"){
				$host = 	$_SERVER['HTTP_HOST']."/subkamol";
			}else{
				$host = $_SERVER['HTTP_HOST'];
			}
			$cment  = Q2( " select * from (
				select i.img_pic , p.pd_id , p.pd_name , p.pd_name2 , l.log_ip , l.log_date , count(*) as pview from log l
				 inner join product p on p.pd_id = l.log_id 
				 left  join images i on i.na_id = p.pd_id
				 where l.tables = 'product' 
				 group by p.pd_id 
				 order by l.log_date DESC 
			) as sub
			order by  ".$sort."
			");
			$i = 1;//print_r($cment);
			/*echo "  select * from (
				select i.img_pic , p.pd_id , p.pd_name , p.pd_name2 , l.log_ip , l.log_date , count(*) as pview from log l
				 inner join product p on p.pd_id = l.log_id 
				 left  join images i on i.na_id = p.pd_id
				 where l.tables = 'product' 
				 group by p.pd_id 
				 order by l.log_date DESC 
			) as sub
			order by  ".$sort."
			";*/
			?>
            <table border = 1 style = "border:solid 1px #CCC; border-collapse:collapse;">
                        	<tr>
                            	<th><a href = "#">รูปผลิตภัณฑ์</a></th>
                            	<th><a href = "#">ชื่อผลิตภัณฑ์</a></th>
                                <th>
                                	<?
                                    	if($_SESSION['pviewsort'] == "DESC"){
											$_SESSION['pviewsort'] = "ASC";
										}else if($_SESSION['pviewsort'] == "ASC"){
											$_SESSION['pviewsort'] = "DESC";
										}else{
											$_SESSION['pviewsort'] = "ASC";
										}
										
										if($_SESSION['datesort'] == "DESC"){
											$_SESSION['datesort'] = "ASC";
										}else if($_SESSION['datesort'] == "ASC"){
											$_SESSION['datesort'] = "DESC";
										}else{
											$_SESSION['datesort'] = "ASC";
										}
									?>
                                	<a href = "?body=pview&pview=<?=$_SESSION['pviewsort']?>">จำนวนวิว</a>
                                </th>
                                <th><a href = "#">รหัสผู้เข้าชมล่าสุด</a></th>
                                <th><a href = "?body=pview&date=<?=$_SESSION['datesort']?>">วันที่เข้าชมล่าสุด</a></th>
                            </tr>
          <?
			foreach($cment as $c){
		?>
				 <tr>
                 		<td>
                        	<?
                            	if($c['img_pic'] == ""){
									$c['img_pic'] = "../imgs/no_photo.gif";
								}else{
									$c['img_pic'] = "../imgs/".$c['img_pic'];
								}
							?>
                        	<center><img src = '<?=$c['img_pic']?>' style = "width:30px;height:30px;" /></center>
                        </td>
                 		<td>
                        	<?
			
								$rpic1 = Q1("select img_pic,img_name1,img_id from images where na_id='".$r['pd_id']."'  and tables = 'product'  order by img_order desc limit 1 "); 
								
								if($_SERVER['HTTP_HOST'] == "localhost"){
									$host = $_SERVER['HTTP_HOST']."/subkamol";
								}else{
									$host = $_SERVER['HTTP_HOST'];
								}
							 ?>
                        	<a target = "_new" href = "http://<?=$host."/index.php?pd_id=".$c['pd_id']."&img_id=".$rpic1['img_id']?>"><?=$c['pd_name']." / ".$c['pd_name2']?></a>
                        </td>
                        <td align="center"><?=$c['pview']?></td>
                        <td><?=$c['log_ip']?></td>
                        <td><?=$c['log_date']?></td>
                 		
				 </tr>
		 <?
				$i++;
			}
		 ?>
         </table>
		 <script type="text/javascript">
		 	$(document).ready(function(e) {
				   $(".btnanswer , .btnanswered").click(function(e) {
					   		$('textarea').removeAttr('name');
							$('.answer_panel:visible').toggle('fast');
							
							$(this).parent().children('.answer_panel:hidden').toggle('fast');
							$(this).parent().find('textarea').attr({'name' : 'des'});
							
					 });
					 
            });
			function addcment(id,pcomment_id){
					var des = $('textarea[name=des]').val();//alert($('textarea[name=des]').val());
					showcment('','' , 'ajax_admin_cment.php?des='+des+'&id='+id+'&pcomment_id='+pcomment_id+'&act=add&tables=product&email=<?=$replyMail?>');	
					$('.answer_panel:visible').parent().children('.btnanswer').addClass('btnanswered');$('.answer_panel:visible').parent().children('.btnanswer').text('answered');
					$('.answer_panel:visible').parent().children('.btnanswer').removeClass('btnanswer');
			}
			function delcment(pcomment_id){
					deletecment('','' , 'ajax_admin_cment.php?pcomment_id='+pcomment_id+'&act=del&tables=product');
			}
		 </script>
          